//
//  CorgiEmoticonBottomView.swift
//  weibo
//
//  Created by Corgi on 17/3/30.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit
// 记录按钮的枚举
enum CorgiEmoticonBottomViewType: Int{
    // 最近
    case recent = 2000
    // 默认
    case normal = 2001
    // emoji
    case emoji = 2002
    // 浪小花
    case lxh = 2003
}
class CorgiEmoticonBottomView: UIStackView {

    var currentBtn: UIButton?
    
    var closure: ((CorgiEmoticonBottomViewType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupCurrentButton(tag: Int) {
        
        let button = viewWithTag(tag + 2000) as! UIButton
        
        if button == currentBtn {
            
            return
        }
        
        currentBtn?.isSelected = false
        
        button.isSelected = true
        
        currentBtn = button
    }
    
    private func setupUI() {
        
//        backgroundColor = randomColor()
        
        axis = .horizontal
        
        distribution = .fillEqually
        
        addChildButtons(title: "最近", imgName: "left", type: CorgiEmoticonBottomViewType.recent)
        addChildButtons(title: "默认", imgName: "mid", type: CorgiEmoticonBottomViewType.normal)
        addChildButtons(title: "Emoji", imgName: "mid", type: CorgiEmoticonBottomViewType.emoji)
        addChildButtons(title: "浪小花", imgName: "right", type: CorgiEmoticonBottomViewType.lxh)
    }
}

extension CorgiEmoticonBottomView {

    fileprivate func addChildButtons(title: String,imgName: String, type: CorgiEmoticonBottomViewType) {
        let button =  UIButton()
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        button.tag = type.rawValue
        button.setTitle(title, for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setTitleColor(UIColor.darkGray, for: UIControlState.selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: FONTSIZEOFNORMAL)
        button.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imgName)_normal"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imgName)_selected"), for: UIControlState.selected)
        // 设置默认的为选中的状态
        if type == .normal {
        
            button.isSelected = true
        
            currentBtn = button
        }
        
        addArrangedSubview(button)
    }

    @objc fileprivate func buttonClick(button: UIButton) {
        
        if button == currentBtn {
            
            return
        }
        
        currentBtn?.isSelected = false
        
        button.isSelected = true
        
        currentBtn = button
        
        closure?(CorgiEmoticonBottomViewType(rawValue: button.tag)!)
    }
}
